<?php
namespace app\index\model;
use think\Model;
use think\Db;
/**
* 
*/
class AuthRule extends Model
{

			// 树形结构
	public function AuthRuleTree(){
      $authRuleRes=$this->order('sort desc')->select();
     
      return  $this->sort($authRuleRes);//对所有栏目排序
    }
    // 排序
    public function sort($data,$pid=0)//$level指定栏目级别
    {
    		static $arr = array();
    		foreach ($data as $k => $v) {
    			if ($v['pid']==$pid) {
                    $v['dataid']=$this->getparentid($v['id']);
    				$arr[]=$v;
    				$this->sort($data,$v['id']); 
    			}
    		}
    		return $arr;
    }

    public function getchilrenid($authRuleId){
            $authRuleRes=$this->select();
            return $this->_getchilrenid($authRuleRes,$authRuleId);
        }

     public function _getchilrenid($authRuleRes,$authRuleId)
        {
            static $arr=array();
            foreach ($authRuleRes as $k => $v) {
                if ($v['pid']==$authRuleId) {
                    $arr[]=$v['id'];
                    $this->_getchilrenid($authRuleRes,$v['id']);
                }
            }
            return $arr;
        }

      public function getparentid($authRuleId){
            $authRuleRes=$this->select();
            return $this->_getparentid($authRuleRes,$authRuleId,True);
        }

     public function _getparentid($authRuleRes,$authRuleId,$clear=false)
        {
            static $arr=array();
            if ($clear) {
                $arr=array();
            }
            foreach ($authRuleRes as $k => $v) {
                if ($v['id']==$authRuleId) {
                    $arr[]=$v['id'];
                    $this->_getparentid($authRuleRes,$v['pid'],False);
                }
            }
            asort($arr);//排序
            $arrStr=implode('-', $arr);

            return $arrStr;
        }












}